package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;



    public User findByUsername(String username) {

        // 获取User
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        // 获取Role
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);

        // 获取Permission
        if(roles != null && roles.size() > 0){
            for(Role role : roles){
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() > 0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }

    //分页查询
    @Override

    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString)  {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.findByUsernames(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    //新增用户
    public void add(User user, Integer[] roles) {
        userDao.add(user);
        if(roles !=null&& roles.length>0){
            //绑定用户和角色的关系
            setUserAndRole(user.getId(),roles);
        }
    }

    //绑定用户和角色的多对多关系
    private void setUserAndRole( Integer id, Integer[] roles){
        for (Integer checkgroupId : roles) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("user_id",id);
            map.put("rold_id",checkgroupId);
            userDao.setUserAndRole(map);
        }
    }

    @Override
    // 根据用户id查询
    public User findByuSerId(Integer id) {
        return userDao.findByUserId(id);
    }

    @Override
    //编辑用户，同时需要更新和检查项的关联关系
    public void edit(User user, Integer[] roles) {
        //根据用户id删除中间表数据
        userDao.deleteAssociation(user.getId());
        //向中间表插入数据
        setUserAndRole(user.getId(),roles);
        //更新用户基本信息
        userDao.edit(user);
    }

    @Override
    //删除
    public void delete(Integer id) {
        //根据用户id删除中间表数据
        userDao.deleteAssociation(id);
        //根据用户id删除用户数据
        userDao.deleteUserId(id);
    }
}